package cn.gailvlun.gll.presentation.wish.maker;

import android.app.Dialog;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.blankj.utilcode.util.KeyboardUtils;
import com.jakewharton.rxbinding2.widget.RxTextView;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityMakeWishBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.wish.MakeWish;
import cn.gailvlun.gll.net.wish.MakeWishReq;
import cn.gailvlun.gll.net.wish.WishTemplate;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.wish.ViewModelFactory;
import cn.gailvlun.gll.presentation.wish.drawer.WishListActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;



public class MakeWishActivity extends BaseActivity {

    private ActivityMakeWishBinding mBinding;
    private MakeWishReq req;
    private MakeWishViewModel mViewModel;

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, MakeWishActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_make_wish);
        mViewModel = ViewModelProviders.of(this, ViewModelFactory.Companion.getInstance()).get(MakeWishViewModel.class);
        mBinding.setVm(mViewModel);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {

        mBinding.navigationBar.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.navigationBar.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {

                req.setText(mBinding.cevContent.getText());
                req.setDayNum(mBinding.tsbDayNum.getPosition());
                makeWish(req);
            }
        });

        RxTextView.textChanges(mBinding.cevContent.getEditText())
                .subscribe(new Consumer<CharSequence>() {
                    @Override
                    public void accept(CharSequence c) throws Exception {
                        boolean enable = !IsEmpty.string(c);
                        mBinding.navigationBar.setMenuEnable(enable);
                        mBinding.navigationBar.setMenuTextColor(
                                enable ? R.color.apricot : R.color.pinkishGrey
                        );
                    }
                });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        req = new MakeWishReq();
    }

    private void makeWish(MakeWishReq req) {
        if (req == null || IsEmpty.string(req.getText().trim())) {
            ToastUtil.showShort("内容不能为空哦～");
            return;
        }
        final Dialog pd = DialogUtil.showIndeterminate(mContext);
        HttpMethods.getWishService()
                .makeWish(req)
                .compose(RxUtil.<MakeWish>applyScheduler())
                .subscribe(new HttpSubscriber<MakeWish>() {
                    @Override
                    protected void onSuccess(MakeWish response) {
                        pd.dismiss();
                        ToastUtil.showShort(getString(R.string.wish_maker_success));
                        WishListActivity.openActivity(mContext);
                        finish();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable MakeWish response, int code) {
                        pd.dismiss();
                        DialogUtil.showError(mContext, errMsg);

                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (KeyboardUtils.isSoftInputVisible(mContext)) {
            KeyboardUtils.hideSoftInput(mContext);
        }
    }

    public class Presenter {
        public ObservableField<String> textObservable = new ObservableField<String>("");

        public void jumpExplain() {
            WishExplainActivity.openActivity(mContext, false);
        }

        public void onPickup() {
            getTemplate();
            if (!KeyboardUtils.isSoftInputVisible(mContext)) {
                KeyboardUtils.showSoftInput(mContext);
            }
        }

        private void getTemplate() {
            HttpMethods.getWishService()
                    .getTemplate()
                    .compose(RxUtil.<WishTemplate>applyScheduler())
                    .subscribe(new HttpSubscriber<WishTemplate>() {

                        @Override
                        protected void onSuccess(WishTemplate response) {
                            if (response != null) {

                                textObservable.set(response.getText());
                            }
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable WishTemplate response, int code) {

                        }
                    });
        }
    }
}
